# _*_ coding : utf-8 _*_
# @Time : 2023/8/26 17:02
# @Author : 寻梦
# @File : 11_handler的基本使用
# @Project : code
import urllib.request

base_url="http://www.baidu.com"

# 创建请求对象
headers={
    "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36 SLBrowser/8.0.1.5162 SLBChan/30"
}

request=urllib.request.Request(url=base_url,headers=headers)

# (1)创建handeler
handler=urllib.request.HTTPHandler()

# (2)获取opener对象
opener= urllib.request.build_opener(handler)

# 调用open方法，获取响应内容
response=opener.open(request)

content=response.read().decode('utf8')

print(content)

